package com.itheima.controller;

import com.itheima.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import java.util.Date;
import java.util.Map;

/**
 * 常用的注解
 */
@Controller
@RequestMapping("/anno")
@SessionAttributes("msg")
public class AnnoController {

    /**
     * 测试RequestParam
     * @param username
     * @return
     */
    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(name = "name") String username){
        System.out.println("testRequestParam.....执行了");
        System.out.println("名字："+username);
        return "success";
    }

    /**
     *测试RequestBody
     * @param boy
     * @return
     */
    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String boy){
        System.out.println("testRequestBody.....执行了");
        System.out.println("请求体："+boy);
        return "success";
    }

    /**
     *PathVariable注解
     * @param string
     * @return
     */
    @RequestMapping("/testPathVariable/{str}")
    public String testPathVariable(@PathVariable(name = "str") String string){
        System.out.println("testPathVariable.....执行了");
        System.out.println(string);
        return "success";
    }

    /**
     * 测试RequestHeader注解
     * @param header
     * @return
     */
    @RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader("Accept") String header){
        System.out.println("testPathVariable.....执行了");
        System.out.println("请求头的值："+header);
        return "success";
    }
    @RequestMapping("/testCookieValue")
    public String testCookieValue(@CookieValue(value = "JSESSIONID") String cookieValue){
        System.out.println("testPathVariable.....执行了");
        System.out.println(cookieValue);
        return "success";
    }

    /**
     * ModelAttribute注解
     * @return
     */
    @RequestMapping("/testModelAttribute")
    public String testModelAttribute(@ModelAttribute("abc") User user){
        System.out.println("testPathVariable.....执行了");
        System.out.println(user);
        return "success";
    }

    @ModelAttribute
    public void showUser(String uname, Map<String,User> map){
        System.out.println("showUser.....执行了");
        //通过用户查询数据库
        User user = new User();
        user.setUname(uname);
        user.setAge(21);
        user.setDate(new Date());
        map.put("abc",user);
    }
//    /**
//     * 该方法会先执行的
//     * @param uname
//     * @return
//     */
//    @ModelAttribute
//    public User showUser(String uname){
//        System.out.println("showUser.....执行了");
//        //通过用户查询数据库
//        User user = new User();
//        user.setUname(uname);
//        user.setAge(21);
//        user.setDate(new Date());
//        return user;
//    }


    /**
     * SessionAttributes注解
     * @return
     */
    @RequestMapping("/testSessionAttributes")
    public String testSessionAttributes(Model model){
        System.out.println("testSessionAttributes.....执行了");
        model.addAttribute("msg","testSessionAttributes");
        return "success";
    }

    /**
     * sessionz中取值
     * @param modelMap
     * @return
     */
    @RequestMapping("/getSessionAttributes")
    public String getSessionAttributes(ModelMap modelMap){
        System.out.println("getSessionAttributes.....执行了");
        Object msg = modelMap.get("msg");
        System.out.println(msg);
        return "success";
    }

    /**
     * 删除session中的值
     * @param status
     * @return
     */
    @RequestMapping("/delSessionAttributes")
    public String delSessionAttributes(SessionStatus status){
        System.out.println("delSessionAttributes.....执行了");
        status.setComplete();
        return "success";
    }
}
